package com.prs.crm.action.cdxh;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.cdxh.HrPerson;
import com.prs.crm.domain.cdxh.ReDispation;
import com.prs.crm.domain.cdxh.salary.DefineSalary;
import com.prs.crm.domain.cdxh.wages.MaterialWages;
import com.prs.crm.domain.cdxh.wages.MoldingWages;
import com.prs.crm.domain.cdxh.wages.PrintWages;
import com.prs.crm.domain.hr.Salary;
import com.prs.crm.domain.hr.SalaryRelat;
import com.prs.crm.service.cdxh.DefineSalaryService;
import com.prs.crm.service.cdxh.HrPersonService;
import com.prs.crm.service.cdxh.MaterialWagesService;
import com.prs.crm.service.cdxh.MoldingWagesService;
import com.prs.crm.service.cdxh.PrintWagesService;
import com.prs.crm.service.cdxh.ReDispationService;
import com.prs.crm.service.hr.SalaryService;

/**
 * @author LanXiaoGang
 * @E-mail:lan9liang@163.com
 * @qq:7857553326
 * @version 创建时间：2010-12-17 上午10:56:28 简单说明
 */

public class DefineSalaryAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private PaginationSupport pagination;

	private HrPersonService hrPersonService;

	private List<Salary> salaryList;
	
	private SalaryRelat salaryRelat;

	private SalaryService salaryService;

	private DefineSalaryService defineSalaryService;

	private String styleVlue;

	private DefineSalary defineSalary=new DefineSalary();

	private String persons;

	private String[] types;
	
	private int loop=0;

	private Double[] prices;

	private String productMoney = "0.0";

	private String productMoneyA = "0.0";

	private ReDispationService reDispationService;

	private MaterialWagesService materialWagesService;

	private MoldingWagesService moldingWagesService;

	private PrintWagesService printWagesService;

	private List<DefineSalary> akList = new ArrayList<DefineSalary>();

	private String allids = "";

	private String pagetrunNum = "";

	@Action("createSalaryBill")
	public String createSalaryBill() {
		salaryList = this.getSalaryService().getAllSalary();
		return SUCCESS;
	}

	@Action("editDefineSalary")
	public String editDefineSalary() {
		String productName = defineSalary.getSalPersonName();
		String productYM = defineSalary.getSalMonth() + "/"
				+ defineSalary.getSalYear();
		productMoney = getReckon(productName, productYM);// 生产奖惩金额
		productMoneyA = getReckonReturn(productName, productYM);// 生产奖惩金额
		return SUCCESS;
	}

	@Action("salaryPerson")
	public String salaryPerson() {
		// System.out.println("styleVlue=" + styleVlue);
		String ssV = "";
		if ("PL".equals(styleVlue)) {
			ssV = "配料组";
		} else if ("CX".equals(styleVlue)) {
			ssV = "成型组";
		} else if ("YS".equals(styleVlue)) {
			ssV = "印刷组";
		}
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				HrPerson.class);
		criteria.add(Restrictions.eq("hrDepartment", ssV));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getHrPersonService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listDefineSalary")
	public String listDefineSalary() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				DefineSalary.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDefineSalaryService().listByPage(criteria,
				pagination));

		return SUCCESS;
	}

	@Action("listDefineSalaryPass")
	public String listDefineSalaryPass() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				DefineSalary.class);
		criteria.add(Restrictions.eq("registState", 2));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDefineSalaryService().listByPage(criteria,
				pagination));

		return SUCCESS;
	}

	/**
	 * 創建工資表
	 * 
	 * @return
	 */
	@Clew(results = { "success" })
	@Action(value = "saveDefineSalary", results = {
			@Result(name = "success", type = "redirect", location = "listDefineSalary"),
			@Result(name = "error", type = "chain", location = "listDefineSalary") })
	public String saveDefineSalary() {

		System.out.println("defineSalary大小值=" + defineSalary);
		this.getDefineSalaryService().saveDefineSalary(defineSalary, persons);
		this.setPrompt("创建工资表成功!");
		return SUCCESS;
	}

	/**
	 * 工资单登记
	 * 
	 * @return
	 */
	@Clew(results = { "success" })
	@Action(value = "saveRegistSalary", results = {
			@Result(name = "success", type = "redirect", location = "listDefineSalary"),
			@Result(name = "error", type = "chain", location = "listDefineSalary") })
	public String saveRegistSalary() {
		System.out.println("productMoney:" + productMoney);
		System.out.println("productMoneyA:" + productMoneyA);
		double allMon = Double.parseDouble(productMoney)
				- Double.parseDouble(productMoneyA);
		this.getDefineSalaryService().saveAlldefineSalarys(defineSalary,
				allMon, types, prices);
		this.setPrompt("工资单登记成功");
		return SUCCESS;
	}

	@Action("viewDefineSalary")
	public String viewDefineSalary() {
		return SUCCESS;
	}

	/**
	 * 工资单打印
	 * 
	 * @return
	 */
	@Action("viewDefinSalary")
    public String viewDefinSalary(){
    	String id[]=pagetrunNum.split(",");
    	for(String z:id)
    	{
    		if(!z.equals(""))
    		{
    			
    DefineSalary ds=this.getDefineSalaryService().get(Integer.parseInt(z));
    akList.add(ds); 			
    			
    			
    			
    		}
    	}
   int x=0; 	
   for(DefineSalary kk:akList)
   {
	   int Nsize=kk.getSalCriterion().getRelats().size();
	   if(Nsize>x)
	   {
		x= Nsize ;
		defineSalary=kk;
	   }
   } 	
    	
    	return SUCCESS;
    }

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public DefineSalaryService getDefineSalaryService() {
		return defineSalaryService;
	}

	public void setDefineSalaryService(DefineSalaryService defineSalaryService) {
		this.defineSalaryService = defineSalaryService;
	}

	public DefineSalary getDefineSalary() {
		return defineSalary;
	}

	public void setDefineSalary(DefineSalary defineSalary) {
		this.defineSalary = defineSalary;
	}

	public List<Salary> getSalaryList() {
		return salaryList;
	}

	public void setSalaryList(List<Salary> salaryList) {
		this.salaryList = salaryList;
	}

	public SalaryService getSalaryService() {
		return salaryService;
	}

	public void setSalaryService(SalaryService salaryService) {
		this.salaryService = salaryService;
	}

	public HrPersonService getHrPersonService() {
		return hrPersonService;
	}

	public void setHrPersonService(HrPersonService hrPersonService) {
		this.hrPersonService = hrPersonService;
	}

	public String getStyleVlue() {
		return styleVlue;
	}

	public void setStyleVlue(String styleVlue) {
		this.styleVlue = styleVlue;
	}

	public String getPersons() {
		return persons;
	}

	public void setPersons(String persons) {
		this.persons = persons;
	}

	public String[] getTypes() {
		return types;
	}

	public void setTypes(String[] types) {
		this.types = types;
	}

	public Double[] getPrices() {
		return prices;
	}

	public void setPrices(Double[] prices) {
		this.prices = prices;
	}

	public String getProductMoney() {
		return productMoney;
	}

	public void setProductMoney(String productMoney) {
		this.productMoney = productMoney;
	}

	public MaterialWagesService getMaterialWagesService() {
		return materialWagesService;
	}

	public void setMaterialWagesService(
			MaterialWagesService materialWagesService) {
		this.materialWagesService = materialWagesService;
	}

	public MoldingWagesService getMoldingWagesService() {
		return moldingWagesService;
	}

	public void setMoldingWagesService(MoldingWagesService moldingWagesService) {
		this.moldingWagesService = moldingWagesService;
	}

	public PrintWagesService getPrintWagesService() {
		return printWagesService;
	}

	public void setPrintWagesService(PrintWagesService printWagesService) {
		this.printWagesService = printWagesService;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getProductMoneyA() {
		return productMoneyA;
	}

	public void setProductMoneyA(String productMoneyA) {
		this.productMoneyA = productMoneyA;
	}

	public ReDispationService getReDispationService() {
		return reDispationService;
	}

	public void setReDispationService(ReDispationService reDispationService) {
		this.reDispationService = reDispationService;
	}

	/**
	 * 人员奖励惩罚制度
	 */

	public String getReckon(String Name, String dataTime) {

		double aLL_SJ = 0.0;// 总计实际产量
		double aLL_Fl = 0.0;// 总计废料

		/**
		 * 配料组
		 */

		List<MaterialWages> PLlist = this.getMaterialWagesService()
				.getMaterialWages(dataTime, Name);// 查询出该姓名的所有记录

		// 符合当月的记录
		List<MaterialWages> Mlist = new ArrayList<MaterialWages>();
		for (MaterialWages m : PLlist) {
			String Moths = m.getRegDate();
			String monT[] = Moths.split("/");
			String MmM = monT[1] + "/" + monT[2];
			if (dataTime.equals(MmM)) {
				Mlist.add(m);
			}
		}

		// 每天统计
		List<MaterialWages> Blist = new ArrayList<MaterialWages>();
		for (int i = 1; i < 33; i++) {
			String x = i + "";
			for (MaterialWages m : Mlist) {
				String curDate[] = (m.getRegDate()).split("/");
				if (curDate[0].equals(x.trim())) {
					Blist.add(m);
				}
			}
			// 实际产量和废料计算 每天的
			// --实际产量
			double priviNum_sj = 0.0;// 规定数量
			double priviPriceA_sj = 0.0;// 奖励单价
			double priviPriceB_sj = 0.0;// 惩罚单价
			double priviductNum_sj = 0.0;// 实际生产数量
			// --废料
			double priviNum_fl = 0.0;// 规定数量
			double priviPriceA_fl = 0.0;// 奖励单价
			double priviPriceB_fl = 0.0;// 惩罚单价
			double priviductNum_fl = 0.0;// 实际生产数量

			for (MaterialWages C : Blist) {
				// 实际产量
				if ("实际产量".equals(C.getProStyles())) {
					priviNum_sj = Double.parseDouble(C.getProvideNums());
					priviPriceA_sj = C.getPoundPrice();
					priviPriceB_sj = C.getRwardPrice();
					priviductNum_sj = priviductNum_sj + C.getThNums();
				} else if ("废料".equals(C.getProStyles())) {
					// 废料
					priviNum_fl = Double.parseDouble(C.getProvideNums());
					priviPriceA_fl = C.getPoundPrice();
					priviPriceB_fl = C.getRwardPrice();
					priviductNum_fl = priviductNum_fl + C.getThNums();
				}
			}

			// 进行比较

			double cz =  priviductNum_sj-priviNum_sj;// 实际
			double cd = priviNum_fl-priviductNum_fl;// 废料

			// 实际产量
	
			if (cz > 0) {
				aLL_SJ = aLL_SJ + (priviPriceA_sj * cz);
			} else if (cz < 0) {
				aLL_SJ = aLL_SJ + (cz * priviPriceB_sj);
			}
	
			if (cd > 0) {
				aLL_SJ = aLL_SJ + (priviPriceA_fl * cd);
			} else if (cd < 0) {
				aLL_SJ = aLL_SJ + (cd * priviPriceB_fl);
			}
			Blist.clear();// 清空Blist集合

		}

	
		/**
		 * 成型组
		 * 
		 */
		 double cxAll=0.0;
		List<MoldingWages> PLlist_c = this.getMoldingWagesService()
				.getMoldingWages(dataTime, Name);// 查询出该姓名的所有记录

		// 符合当月的记录
		List<MoldingWages> Mlist_c = new ArrayList<MoldingWages>();
		for (MoldingWages m : PLlist_c) {
			String Moths = m.getRegDate();
			String monT[] = Moths.split("/");
			String MmM = monT[1] + "/" + monT[2];
			if (dataTime.equals(MmM)) {
				Mlist_c.add(m);
			}
		}

		// 每天统计
		List<MoldingWages> Blist_c = new ArrayList<MoldingWages>();
		for (int i = 1; i < 33; i++) {
			String x = i + "";
			for (MoldingWages m : Mlist_c) {
				String curDate[] = (m.getRegDate()).split("/");
				if (curDate[0].equals(x.trim())) {
					Blist_c.add(m);
				}
			}
			// 实际产量和废料计算 每天的
			// --实际产量
			double priviNum_sj_c = 0.0;// 规定数量
			double priviPriceA_sj_c = 0.0;// 奖励单价
			double priviPriceB_sj_c = 0.0;// 惩罚单价
			double priviductNum_sj_c = 0.0;// 实际生产数量
			// --废料
			double priviNum_fl_c = 0.0;// 规定数量
			double priviPriceA_fl_c = 0.0;// 奖励单价
			double priviPriceB_fl_c = 0.0;// 惩罚单价
			double priviductNum_fl_c = 0.0;// 实际生产数量

			for (MoldingWages C : Blist_c) {
				// 实际产量
				if ("实际产量".equals(C.getProStyles())) {
					priviNum_sj_c = Double.parseDouble(C.getProvideNums());
					priviPriceA_sj_c = C.getPoundPrice();
					priviPriceB_sj_c = C.getRwardPrice();
					priviductNum_sj_c = priviductNum_sj_c + C.getThNums();
				} else if ("废料".equals(C.getProStyles())) {
					// 废料
					priviNum_fl_c = Double.parseDouble(C.getProvideNums());
					priviPriceA_fl_c = C.getPoundPrice();
					priviPriceB_fl_c = C.getRwardPrice();
					priviductNum_fl_c = priviductNum_fl_c + C.getThNums();
				}
			}

			// 进行比较

			double cz = priviductNum_sj_c-priviNum_sj_c;// 实际
			double cd = priviNum_fl_c-priviductNum_fl_c;// 废料

			// 实际产量
		
			if (cz > 0) {
				cxAll = cxAll + (priviPriceA_sj_c * cz);
			} else if (cz < 0) {
				cxAll = cxAll + (cz * priviPriceB_sj_c);
			}
			// 废料
			if (cd > 0) {
				cxAll = cxAll + (priviPriceA_fl_c * cd);
			} else if (cd < 0) {
				cxAll = cxAll + (cd * priviPriceB_fl_c);
			}
			Blist_c.clear();// 清空Blist集合

		}
		
		 
		/**
		 * 印刷组
		 */
        double ysAll=0.0;
		List<PrintWages> PLlist_y = this.getPrintWagesService().getPrintWages(
				dataTime, Name);// 查询出该姓名的所有记录
   System.out.println("查出值大小:"+PLlist_y.size());
		// 符合当月的记录
		List<PrintWages> Mlist_y = new ArrayList<PrintWages>();
		for (PrintWages m : PLlist_y) {
			String Moths = m.getRegDate();
			String monT[] = Moths.split("/");
			String MmM = monT[1] + "/" + monT[2];
			if (dataTime.equals(MmM)) {
				Mlist_y.add(m);
			}
		}
System.out.println("当天大小:"+Mlist_y.size());
		// 每天统计
		List<PrintWages> Blist_y = new ArrayList<PrintWages>();
		for (int i = 1; i < 33; i++) {
			String x = i + "";
			for (PrintWages m : Mlist_y) {
				System.out.println("m.getRegDate():"+m.getRegDate());
				String curDate[] = (m.getRegDate()).split("/");
				System.out.println("时间A："+curDate[0]);
				System.out.println("时间B："+x);
				if (curDate[0].equals(x.trim())) {
					Blist_y.add(m);
				}
			}
			
		System.out.println("B的值:"+Blist_y.size());	
		
			// 实际产量和废料计算 每天的
			// --实际产量
			double priviNum_sj_y = 0.0;// 规定数量
			double priviPriceA_sj_y = 0.0;// 奖励单价
			double priviPriceB_sj_y = 0.0;// 惩罚单价
			double priviductNum_sj_y = 0.0;// 实际生产数量

			for (PrintWages C : Blist_y) {
				// 实际产量

				priviNum_sj_y = Double.parseDouble(C.getProvideNums());
				priviPriceA_sj_y = C.getPoundPrice();
				priviPriceB_sj_y = C.getRwardPrice();
				priviductNum_sj_y = priviductNum_sj_y + C.getThNums();

			}

			System.out.println("cz priviductNum_sj_y:"+priviductNum_sj_y);
			System.out.println("cz priviNum_sj_y:"+priviNum_sj_y);
			// 进行比较
			double cz = priviductNum_sj_y-priviNum_sj_y;// 实际
			System.out.println("cz cz:"+cz);
			System.out.println("奖励单价 CX："+priviPriceA_sj_y);
			System.out.println("惩罚单价 CX："+priviPriceB_sj_y);
			// 实际产量
			if (cz > 0) {
				ysAll = ysAll + (priviPriceA_sj_y * cz);
			} else if (cz < 0) {
				ysAll = ysAll + (cz * priviPriceB_sj_y);
			}

			Blist_y.clear();// 清空Blist集合

		}
		
		System.out.println("ysAll:"+ysAll);
		
		double overs=ysAll+cxAll+aLL_SJ;
		
		return overs + "";
	}

	public String getReckonReturn(String name, String dATE) {
		double allMony = 0.0;// 总金额
		List<ReDispation> alist = this.getReDispationService()
				.getReDispationWages(name);

		// 天数过滤
		List<ReDispation> Blist = new ArrayList<ReDispation>();
		String currtrn[] = dATE.split("/");
		for (ReDispation R : alist) {
			String Dta[] = (R.getReDate()).split("/");
			if (Dta[1].equals(currtrn[0])) {
				Blist.add(R);
			}
		}

		for (ReDispation RR : Blist) {
			allMony = allMony
					+ (Double.parseDouble(RR.getOprice()) * Double
							.parseDouble(RR.getTkNumbers()));
		}

		return allMony + "";
	}

	public List<DefineSalary> getAkList() {
		return akList;
	}

	public void setAkList(List<DefineSalary> akList) {
		this.akList = akList;
	}

	public String getAllids() {
		return allids;
	}

	public void setAllids(String allids) {
		this.allids = allids;
	}

	public String getPagetrunNum() {
		return pagetrunNum;
	}

	public void setPagetrunNum(String pagetrunNum) {
		this.pagetrunNum = pagetrunNum;
	}

}
